package com.tv.cms.utils;

import okhttp3.*;
import org.apache.http.util.TextUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @project tvcms
 * @description
 * @date 2023/7/19 23:22:20
 */
public class HttpUtils {
    private static final OkHttpClient client = new OkHttpClient();

    /**
     * GET请求
     * @param url
     * @return
     * @throws IOException
     */
    public static String sendGetRequest(String url,Map<String,Object> map) throws IOException {
        String urlNew = url;
        // 设置HTTP请求参数
        urlNew += getBodyParams(map);
        urlNew+="&access_output[]=2&access_output[]=3";
        Request request = new Request.Builder()
                .url(urlNew)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    /**
     * GET请求
     * @param url
     * @return
     * @throws IOException
     */
    public static String sendGetRequest2(String url,Map<String,Object> map) throws IOException {
        String urlNew = url;
        // 设置HTTP请求参数
        urlNew += getBodyParams(map);
        Request request = new Request.Builder()
                .url(urlNew)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }


    /**
     * 添加参数
     *
     * @param bodyParams
     * @return
     */
    private static String getBodyParams(Map<String, Object> bodyParams) {
        //1.添加请求参数
        //遍历map中所有参数到builder
        if (bodyParams != null && bodyParams.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer("?");
            for (String key : bodyParams.keySet()) {
                if (!TextUtils.isEmpty(key) && bodyParams.get(key) != null && !TextUtils.isEmpty(bodyParams.get(key).toString())) {
                    //如果参数不是null并且不是""，就拼接起来
                    stringBuffer.append("&");
                    stringBuffer.append(key);
                    stringBuffer.append("=");
                    stringBuffer.append(bodyParams.get(key));
                }
            }

            return stringBuffer.toString();
        } else {
            return "";
        }
    }

    /**
     * 添加headers
     *
     * @param headerParams
     * @return
     */
    private Headers setHeaderParams(Map<String, String> headerParams) {
        Headers headers = null;
        Headers.Builder headersbuilder = new Headers.Builder();
        if (headerParams != null && headerParams.size() > 0) {
            for (String key : headerParams.keySet()) {
                if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(headerParams.get(key))) {
                    //如果参数不是null并且不是""，就拼接起来
                    headersbuilder.add(key, headerParams.get(key));
                }
            }
        }

        headers = headersbuilder.build();
        return headers;
    }

    /**
     * POST请求
     * @param url
     * @param requestBody
     * @return
     * @throws IOException
     */
    public static String sendPostRequest(String url, String requestBody) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), requestBody);

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    /**
     * PUT请求
     * @param url
     * @param requestBody
     * @return
     * @throws IOException
     */
    public static String sendPutRequest(String url, String requestBody) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), requestBody);

        Request request = new Request.Builder()
                .url(url)
                .put(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    /**
     * POST表单请求
     * @param url
     * @param formBuilder
     * @return
     * @throws IOException
     */
    public static String sendFormPostRequest(String url, FormBody.Builder formBuilder, String cookie) throws IOException {
        RequestBody requestBody = formBuilder.build();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Cookie", cookie)
                .post(requestBody)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static String sendGetRequest3(String url,Map<String,Object> map) throws IOException {
        String urlNew = url;
        // 设置HTTP请求参数
        urlNew += getBodyParams(map);
        urlNew+="&access_output[]=2&access_output[]=3";
        Request request = new Request.Builder()
                .url(urlNew)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}
